
import { _decorator, Collider, Component, ITriggerEvent, Node } from 'cc';
import { Constant } from '../framework/Constant';
import { GameManager } from '../framework/GameManager';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = EnemyPlane
 * DateTime = Tue Feb 27 2024 15:31:02 GMT+0800 (中国标准时间)
 * Author = kun12303
 * FileBasename = EnemyPlane.ts
 * FileBasenameNoExtension = EnemyPlane
 * URL = db://assets/script/plane/EnemyPlane.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/en/
 *
 */
const OUTOFBOUNCE = 50;
 
@ccclass('EnemyPlane')
export class EnemyPlane extends Component {
    @property
    public createBulletTime = 0.5

    private _enemySpeed = 0;
    private _needBullet = false;
    private _gameManager:GameManager = null;

    private _currCreateBulletTime = 0;

    start () {
        // [3]
    }

    protected onEnable(): void {
        const collider = this.getComponent(Collider)
        collider.on('onTriggerEnter',this._onTriggerEnter,this)
    }

    protected onDisable(): void {
        const collider = this.getComponent(Collider)
        collider.off('onTriggerEnter',this._onTriggerEnter,this)
    }

    update (deltaTime: number) {
        const pos = this.node.position;
        const movePos = pos.z + this._enemySpeed
        this.node.setPosition(pos.x, pos.y, movePos);

        if(this._needBullet){
            this._currCreateBulletTime+=deltaTime;
            if(this._currCreateBulletTime>this.createBulletTime){
                this._gameManager.createEnemyBullet(this.node.position)
                this._currCreateBulletTime = 0
            }
        }
        if(movePos>OUTOFBOUNCE){
            PoolManager.instance().putNode(this.node);
            // this.node.destroy()
        }
    }

    _onTriggerEnter(event:ITriggerEvent){
        const collisionGroup = event.otherCollider.getGroup();
        if(collisionGroup===Constant.CollisionType.SELF_PLANE||collisionGroup===Constant.CollisionType.SELF_BULLET){
            this._gameManager.playAudioEffect('enemy')
            PoolManager.instance().putNode(this.node);
            // this.node.destroy()
            this._gameManager.addScore()
            this._gameManager.createEnemyEffect(this.node.position)
        }
    }

    show(gameManager:GameManager,speed:number,needBullet:boolean){
        this._gameManager = gameManager;
        this._enemySpeed = speed;
        this._needBullet = needBullet
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/en/scripting/life-cycle-callbacks.html
 */
